<?php

/*
 * Copyright 2008 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#namespace tehframework\iocc;

#use \InvalidArgumentException;

abstract class Binding
{
	/**
	 * @var string
	 */
	private $key;
	
	/**
	 * @var array<string>
	 */
	private $types;
	
	/**
	 * @var string?
	 */
	private $name;
	
	/**
	 * @var string?
	 */
	private $scope;
	
	/**
	 * @var callable?
	 */
	private $provider;
	
	/**
	 * @return ObjectDefinition
	 */
	abstract public function getDefinition();
	
	/**
	 * @param  string|array<string> $types
	 * @param  string? $name
	 * @param  string? $scope
	 * @param  callable? $provider
	 * @throws InvalidArgumentException
	 */
	public function __construct($types, $name = null, $scope = null, $provider = null)
	{
		if (empty($types) || !isset($types[0]))
		{
			throw new InvalidArgumentException('At least one type must be specified (implementation type).');
		}
		
		$this->types    = (array)$types;
		$this->name     = $name;
		$this->scope    = $scope;
		$this->provider = $provider;
	}
	
	/**
	 * @return string
	 */
	public function getKey()
	{
		if ($this->key === null)
		{
			$this->key = (string)crc32($this->types[0] . $this->name);
		}
		
		return $this->key;
	}
	
	/**
	 * @return array<string>
	 */
	public function getTypes()
	{
		return $this->types;
	}
	
	/**
	 * @return string?
	 */
	public function getName()
	{
		return $this->name;
	}

	/**
	 * @return string?
	 */
	public function getScope()
	{
		return $this->scope;
	}
	
	/**
	 * @return callable?
	 */
	public function getProvider()
	{
		return $this->provider;
	}
}